package com.hrma.controller;

import com.github.pagehelper.Page;
import com.hrma.config.Permission;
import com.hrma.entity.*;
import com.hrma.services.DepartmentService;
import com.hrma.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Slf4j
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @PostMapping("/add")
    public Result<Department> addDepartment(@Validated @RequestBody Department data) {
        log.debug("收到add请求，请求参数：{}",data);
        return Result.ok(departmentService.addDepartment(data));
    }

    @DeleteMapping("/delete/{id}")
    public Result<?> deleteDepartment(@PathVariable int id) {
        log.debug("收到delete请求，请求参数：{}",id);
        departmentService.deleteDepartment(id);
        return Result.ok(null);
    }

    @PatchMapping("/update")
    public  Result<?> updateDepartment(@Validated @RequestBody Department data) {
        log.debug("收到update请求，请求参数：{}",data);
        departmentService.updateDepartment(data);
        return Result.ok(null);
    }

    @GetMapping("/select")
    public Result<List<SelectResult>> selectDepartment() {
        log.debug("收到select请求!");
        return Result.ok(departmentService.selectDepartment());
    }

    @PostMapping("/getList")
    public Result<ListWithCount<Department>> getList(@RequestBody DepartmentDTO data){
        log.debug("收到getList请求！");
        Page<Department> res = departmentService.getList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }

}
